<?

/** Class to hold a view of a form.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author Jyry Kuukkanen
 *  $Id: dzview.php 595 2006-04-03 16:00:47Z jyry $
 */

include_once("sodebug.php");
include_once("sostreams.php");
include_once("dztemplate.php");
include_once("dzlaituri.php");
include_once("dzconst.php");
include_once("dzgeneric.php");

/** View types. */
define(DZ_VIEW_TYPE_DATAXI, 1);		/** dataxi view */
define(DZ_VIEW_TYPE_PICK_UP, 2);    /** Pick up view */
define(DZ_VIEW_TYPE_DIALOG, 3);     /** informative and confirming views "*/
define(DZ_VIEW_TYPE_PSEUDO, 4);     /** View used to collect information "*/


/** Holds basic information for a form
 *  This is information that is needed to create a new form.
 *  Object type of this is passed as a parameter for pushView()
 *  method in dzViewStack.
 */
class dzViewData {
    var $Type;          /**int type of the view. See defines above */
    var $Disposable;    /**bool May the form be discarted when changeing
                                to an other view stack */
    var $SrcFile;       /**str File name where the form source is located */
    var $System;        /**bool Is this a system view*/

    /** Use constructor to initialize the class
     */
    function dzViewData($Type, $Disposable, $SrcFile, $System = false) {
        $this->Type = $Type;
        $this->Disposable = $Disposable;
        $this->SrcFile = $SrcFile;
        $this->System = $System;
    } // dzViewData
} // dzViewData


/** Single view class
 *  This class is a base class for implementing a view.
 *  This class implements all view types and takes care of the data access and
 *  producing the output code for the client.
 */
class dzView extends soBaseClass {
    var $Laituri = NULL;    /** dzLaituri object that commicates with Kone and
                                contains the data store */
    var $Template = NULL;   /** dzTemplate Contains the genral structure and
                                the properties of the form. */
    var $OutFilter = NULL;  /** dzOutHtml This is the class that produces the
                                output code to send for the client. The type
                                of this property depends on the used form
                                markup language. Currently only html is
                                possible. */


    /** Constructor
     *  Initialize properties
     *  @param object dzSessionInfo $BasicSessionInfo Basic information of the
     *      session this view belongs to
     *  @param string $ViewSrc The source file for the view
     *  @param int $Type View type
     */
    function dzView($BasicSessionInfo, $ViewSrc, $Type = DZ_VIEW_TYPE_DATAXI) {
        /* Extract the struct name and the source type from the view name */
        $struct_name = soExtractVal(basename($ViewSrc), ".", -2);
        $source_type = soExtractVal($ViewSrc, ".", -1);
        $this->Template = new dzTemplate($Type, $source_type,
                                         $ViewSrc, $BasicSessionInfo);
        /* Get the parse result from dzTemplate */
        $this->Error = $this->Template->Error;

        /* No need to proceed if parse failed */
        if ($this->Error != DZ_TMPL_PARSE_FAILED) {
            switch ($Type) {
                case DZ_VIEW_TYPE_PICK_UP:
                case DZ_VIEW_TYPE_PSEUDO:
                case DZ_VIEW_TYPE_DATAXI:
                    $db_alias = $this->Template->getDbAlias();
                    $this->Laituri = new dzLaituri($struct_name,
                                                   $BasicSessionInfo,
                                                   $db_alias);
                    $this->__setupGroups();
                    break;
            } // switch $Type
        }
    } // dzView()


    /** Store the data on the form that was received from the client side.
     *  Picks up the values from the fields and stores them in Tiski.
     *  @param soSet $FormVars Fields and their values on the form
     */
    function storeClientForm($FormVars) {
        soDebug("dzView->storeClientForm() entered", SOD_DET);
        $keys = $this->Template->ClientWidgetMap->getasArray();
        $mode = $this->Template->getViewData(DZ_VI_MODE);

        foreach ($keys as $field_name => $purkki_ref) {
            /* $purkki_ref[0] = entity name
               $purkki_ref[1] = row number */
            $value = $FormVars->getItem($field_name);

            $col_type = $this->Template->getColProperty(
                $purkki_ref[0], DZCPT_STRUCT, DZCP_TYPE);
            /* Store the data from the form to the tiski. */
            $this->Laituri->setStoreItem(TULLI,
                                         $purkki_ref[0], /* Entity name.*/
                                         $purkki_ref[1], /* Row number */
                                         $value);
        }; // foreach

        /* Check & process pre-infilter options */
        $preinfilter = $this->Template->getFormEvent(DZE_PRE_INFILTER,
                                                     DZ_SK_EVAL);
        if (($mode == DZM_EDIT) && $preinfilter) {
#            $this->__preInFilter(TISKI, $Lang, $preinfilter);
        }; // if


        /* Perform type conversion for the data in TULLI. Data will be
           converted to internal format and stored in TISKI. */
        $filter = new dzFilter($this->Template, $this->Laituri);
        $filter->filterIn();
    } // storeClientForm


    /** Processes pre-infilter options.
     *  Pre-infilter options most commonly include calls to custom procedures.
     *  @param storeid $StoreID Store to be validated.
     *  @param string $Lang Language code.
     *  @param string $PreInFilter $Pre-Infilter declaration
     */
    function __preInFilter($StoreID, $Lang, &$PreInFilter) {
        if (!$this->Laituri->isStore($StoreID)) return NULL; // <-- EXIT

        $store = $this->Laituri->getStore($StoreID);
        $errors = array();

        $eval = soSplitStr($PreInFilter, ":", 1);
        if (count($eval)) {
            $eval[0] = str_replace("~", dzGetMainScriptPath(), $eval[0]);
            eval("include_once(\"".$eval[0]."\"); ".
                "\$result_set = ".$eval[1]."(\$store);");
            if (!$result || !is_array($result)) return; // <-- EXIT
        }; // if

        foreach ($result as $key => $value) {
            $col = soExtractVal($key, "#", 1);
            $rowno = intval(soExtractVal($key, "#", 2));
            $this->Laituri->setStoreItem($col, $rowno, $value);
        }; // foreach
    } // __preInFilter



    /** Sets up groups used by Purkki under Laituri.
     *  Loops through each group in Template and sets them up in
     *  Laituri->Purkki accordingly.
     */
    function __setupGroups() {
        $cache = &$this->Template->getCache();
        $this->Laituri->setupGroups($cache->Groups);
    } // setupGroups

}; // class dzView
?>
